package com.example.timeemail.adapter;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.timeemail.R;
import com.example.timeemail.entity.Person;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MyRecyclerAdapter  extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{
    //通讯录数据
    private List<Person> pdata = new ArrayList<Person>();
    //上下文
    private Context context;
    //定义监听
    private OnItemClickListener monItemClickListener;

    public MyRecyclerAdapter(List<Person> pdata, Context context) {
        this.pdata = pdata;
        this.context = context;
    }

    public void setMonItemClickListener(OnItemClickListener monItemClickListener) {
        this.monItemClickListener = monItemClickListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充布局，获取列表项布局文件
        View itemview = LayoutInflater.from(context).inflate(R.layout.driftitem,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(itemview);
        return myViewHolder;
    }

    @Override
    //填充onCreateViewHolder方法返回的holder控件
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Person person = pdata.get(position);
        holder.myimg.setImageResource(person.getImg());
        holder.myname.setText(person.getName());
        //设置监听方法
        if(monItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    monItemClickListener.OnItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    //返回通讯录数据个数
    public int getItemCount() {
        return pdata.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        //定义对应列表项
        private ImageView myimg;
        private TextView myname;
        //构造方法

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            myimg = (ImageView)itemView.findViewById(R.id.item_img);
            myname = (TextView) itemView.findViewById(R.id.item_name);
        }



    }
}
